using BootstrapBlazor.Components;
using BootstrapBlazorApp.Server.Components;
using BootstrapBlazorApp.Server.Service;
using FreeSql;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using System.Text;
using Console = System.Console;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

builder.Services.AddRazorComponents().AddInteractiveServerComponents();

builder.Services.AddBootstrapBlazor();

//注入数据库服务
builder.Services.AddScoped(typeof(IDataService<>), typeof(FreesqlDataService<>));
//注入controller
builder.Services.AddControllers();
//注入认证服务
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(
    config =>
    {
        config.LoginPath = "/Login";
    });
//注入cascding，级联身份验证状态
builder.Services.AddCascadingAuthenticationState();
//添加内存缓存服务
builder.Services.AddSingleton<IMemoryCache, MemoryCache>();

builder.Services.Configure<HubOptions>(option => option.MaximumReceiveMessageSize = null);

IFreeSql fsql = new FreeSqlBuilder()
    .UseConnectionString(DataType.PostgreSQL, @"Host=8.130.48.49;Port=5432;Username=dotnet;Password=adminsoftware; Database=dotnet;ArrayNullabilityMode=Always;Pooling=true;Minimum Pool Size=1")
    .UseMonitorCommand(cmd => Console.WriteLine($"Sql：{cmd.CommandText}"))
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库，只有CRUD时才会生成表
    .Build();   

BaseEntity.Initialization(fsql, null);
// 增加 SignalR 服务数据传输大小限制配置
builder.Services.Configure<HubOptions>(option => option.MaximumReceiveMessageSize = null);
// 增加文件上传服务
builder.Services.AddScoped<UploadService>();
builder.Services.AddScoped<LlmChainService>();
var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseAntiforgery();

//处理身份验证
app.UseAuthentication();
//处理是否相应资源
app.UseAuthorization();
app.MapDefaultControllerRoute();

app.MapRazorComponents<App>().AddInteractiveServerRenderMode();

app.Run();